import { OneModal } from '@/components/one-ui';
import { IOneModalRef } from '@/pages/type';
import { fieldFlowExport, getCompanyID, getLoadMapList, getPosIDs, getTenantID, getUserDepID, getUserID } from '@/utils';
import { Col, Form, Input, message, Row, Select, Spin } from 'antd';
import { Ref, useRef } from 'react';
import pageStyles from '@/common/pageLayout.less';
import React from 'react';
import { ILoadMapTypeAll } from '@/common';
import { exportAssetBudgetExportService } from '../services';

export const ExportModal = React.forwardRef<any, any>(({ onClose, modalTitle, modalWidth = 0.25, searchParams }, ref) => {
  /**
   * modal ref
   */
  const exportModalRef: Ref<IOneModalRef> = useRef(null);

  /**
   * 关闭弹窗
   */
  const onCloseModal = () => {
    if (exportModalRef) {
      if (exportModalRef.current) {
        if (exportModalRef.current.onModalCancel) {
          assetTypeForm.setFieldsValue({});
          exportModalRef.current.onModalCancel();
        }
      }
    }
  };

  /** 表单 form */
  const [assetTypeForm] = Form.useForm();

  /**  获取数据字典 */
  const { loadMapInfo, mapLoading } = getLoadMapList([`${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`]);

  /** 导出回调 */
  const exportAssetBudgetExportCallback = (res: any) => {
    if (res.status) {
      message.error('导出失败！');
      onCloseModal();
    } else {
      /** 处理文件流 */
      fieldFlowExport(res, '预算一体化资产信息数据.xlsx');
      onCloseModal();
    }
  };

  /**
   * 引入导出接口
   */
  const { exportAssetBudgetExportRun, exportAssetBudgetExportLoading } = exportAssetBudgetExportService(exportAssetBudgetExportCallback);

  /**
   * 保存
   */
  const saveCancelOnOk = () => {
    let obj = {};
    assetTypeForm?.validateFields()?.then((values: any) => {
      obj = { ...searchParams, ...values };
      exportAssetBudgetExportRun(obj);
    });
  };

  return (
    <>
      <OneModal
        open={true}
        ref={exportModalRef}
        title={modalTitle}
        width={document.body.clientWidth * modalWidth}
        onCancel={onClose}
        bodyStyle={{
          padding: '5px 0px',
        }}
        centered
        onOk={saveCancelOnOk}
        confirmLoading={exportAssetBudgetExportLoading || mapLoading}
      >
        <Spin spinning={exportAssetBudgetExportLoading || mapLoading}>
          <div className={pageStyles['fromView-styles']}>
            <Form form={assetTypeForm}>
              <Row>
                <Col span={24}>
                  <Form.Item
                    label={'资产类型'}
                    name={'assetType'}
                    rules={[
                      {
                        required: true,
                        message: `请选择资产类型`,
                      },
                    ]}
                  >
                    <Select placeholder={`请选择资产类型`} allowClear loading={mapLoading}>
                      {loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`]?.map((item: any) => {
                        return (
                          <Select.Option key={item?.id} value={item?.id}>
                            {item?.name}
                          </Select.Option>
                        );
                      })}
                    </Select>
                  </Form.Item>
                </Col>
              </Row>
            </Form>
          </div>
        </Spin>
      </OneModal>
    </>
  );
});
